<template>
	<a-card :bordered="false">
		<div class="table-page-search-wrapper">
			<a-form layout="inline">
				<a-row :gutter="48">

					<a-col :md="8" :sm="24">
						<a-form-item label="采购单号">
							<a-input v-model="queryParam.orderNumber" placeholder="请输入采购单号" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24">
						<a-form-item label="物料编码">
							<a-input v-model="queryParam.jzMaterialCode" placeholder="请输入物料编码" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24">
						<a-form-item label="采购时间">
							<a-range-picker style="width:100%" v-model="queryParam.createTime" format="YYYY-MM-DD"
								value-format="YYYY-MM-DD HH:mm:ss"
								:showTime="{ defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')] }" />
						</a-form-item>
					</a-col>

					<a-col :md="!advanced && 8 || 24" :sm="24">
						<span class="table-page-search-submitButtons"
							:style="advanced && { float: 'right', overflow: 'hidden' } || {}">
							<a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
							<!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
						</span>
					</a-col>

					<a-col :md="24" :sm="24" v-if="canReadOrgIdCompany()">
						<a-form-item label="供应商：">
							<!-- <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdSupplier">
								<a-radio-button value="">
									全部
								</a-radio-button>
								<a-radio-button :value="id"
									v-for="{ id, name } in $util.getOrgList([$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id, $businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id])"
									:key="id">
									{{ name }}
								</a-radio-button>
							</a-radio-group> -->
							<FCOrgRadioGroup1 v-model="queryParam.orgIdSupplier" :filter-types="materialType" />
						</a-form-item>
					</a-col>

					<!-- <a-col :md="24" :sm="24">
            <a-form-item label="归属公司：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdCompany">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in $util.getOrgList([$businessEnum.ORG_TYPE_ENUM.COMPANY.id])">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>

            </a-form-item>
          </a-col> -->

				</a-row>
			</a-form>
			<Export text="导出" title="待入库采购单" url="/emsOrder/cgDRKExport" :obj="{
				materialType: materialType,
				orderType: orderType,
				executeStatus: executeStatus,
				orderNumber: queryParam.orderNumber,
				jzMaterialCode: queryParam.jzMaterialCode,
				orgIdSupplier: queryParam.orgIdSupplier,
				startTime: appendTime(queryParam.createTime)[0],
				endTime: appendTime(queryParam.createTime)[1],
			}" />
		</div>

		<s-table ref="table" size="default" :columns="canReadOrgIdCompany() ? columns : columns1" :data="loadData"
			:alert="true" :rowKey="(record) => record.code" :rowSelection="null">
			<span slot="action" slot-scope="text, record">
				<!-- <template v-if="record.targetOrgType == $businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id">
					<a @click="$refs.inStoreFormByNormalSupplier.show(record, materialType)">
						完成入库
					</a>
					<a-divider type="vertical" />
				</template> -->
				<a @click="$refs.entryForm.show(record, { materialType })" style="margin: 10px;">
					去入库
				</a>
				<a @click="$refs.detailForm1.show(record, { materialType })" style="margin: 10px;">
					入库明细
				</a>
				<a @click="$refs.rukuList.show(record, { materialType, orderType, })" style="margin: 10px;">
					入库记录
				</a>
			</span>
		</s-table>

		<in-store-form-by-normal-supplier ref="inStoreFormByNormalSupplier" @ok="handleOk" />
		<detail-form ref="detailForm" />
		<detail-form1 ref="detailForm1" />
		<entry-form ref="entryForm" @ok="handleOk"></entry-form>
		<ruku-list ref="rukuList"></ruku-list>
	</a-card>
</template>

<script>
import { STable } from '@/components'
import { page, } from '@/api/modular/main/emsorder/emsOrderManage'
import { getUrlParam } from '@/utils/util'
import moment from 'moment'

import inStoreFormByNormalSupplier from './inStoreFormByNormalSupplier'
import detailForm from './detailForm'
import detailForm1 from './detailForm1'
import rukuList from './rukuList'

import entryForm from './entryForm.vue'
import FCOrgRadioGroup1 from '@/components/FcOrgRadioGroup/FCOrgRadioGroup1'

export default {
	components: {
		STable,
		inStoreFormByNormalSupplier,
		detailForm,
		entryForm,
		detailForm1,
		rukuList,
		FCOrgRadioGroup1
	},
	data() {
		return {
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			columns: [
				{
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber'
				},
				{
					title: '供应商',
					dataIndex: 'targetOrgName'
				},
				{
					title: '采购时间',
					dataIndex: 'createTime'
				},
				{
					title: '操作',
					dataIndex: 'action',
					width: '300px',
					scopedSlots: { customRender: 'action' }
				}
			],
			columns1: [
				{
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber'
				},
				// {
				//   title: '供应商',
				//   dataIndex: 'targetOrgName'
				// },
				{
					title: '采购时间',
					dataIndex: 'createTime'
				},
				{
					title: '操作',
					dataIndex: 'action',
					width: '300px',
					scopedSlots: { customRender: 'action' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				this.queryParam.materialType = this.materialType
				this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.purchaseOrder.id
				this.queryParam.executeStatuses = this.$businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id			
				this.queryParam.cgApplyType = 1;	
				let _queryParam = Object.assign({}, this.queryParam)
				if (_queryParam.createTime?.[0]) {
					_queryParam.createTimeStart = moment(_queryParam.createTime?.[0]).format('YYYY-MM-DD HH:mm:ss')
				}
				if (_queryParam.createTime?.[1]) {
					_queryParam.createTimeEnd = moment(_queryParam.createTime?.[1]).format('YYYY-MM-DD HH:mm:ss')
				}
				delete _queryParam.createTime
				return page(Object.assign(parameter, _queryParam)).then((res) => {
					return res.data
				})
			},
			scheduleProductionVisible: false,
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			replaceFields: {
				key: 'id',
			},
		}
	},
	computed: {
		routePath() {
			let urlParam = getUrlParam(this.$route.path)
			return urlParam
		},
		materialType() {
			return this.routePath.materialType
		},
		orderType() {
			return this.routePath.orderType
		},
		executeStatus() {
			return this.routePath.executeStatus
		},
	},
	watch: {
		'$route.path': function (newValue) {
			setTimeout(() => {
				this.$refs.table.refresh(true)
				this.$refs.table.clearSelected()
			}, 100)
		}
	},
	methods: {
		moment,
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		canReadOrgIdCompany() {
			return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
				&& !this.hasRole(this.$util.ROLE_ENUM.SQCG.id)
		},
	}
}
</script>
